package leetcode.code019.RemoveNthNodeFromEndofList;

import java.util.LinkedList;
import java.util.List;

import leetcode.helper.tree.ListNode;

/**
 * 一次 + linkedlist
 * @author max
 * @date 2021年8月16日
 * @description
 */
public class Solution1 extends RemoveNthNodeFromEndofList {

	@Override
	public ListNode removeNthFromEnd(ListNode head, int n) {
		List<ListNode> l = new LinkedList<>();
		ListNode p = head;
		while (p != null) {
			l.add(p);
			p = p.next;
		}
		int size = l.size();
		int aim = size - n;
		if (aim - 1 >= 0) {
			ListNode pre = l.get(aim - 1);
			pre.next = pre.next.next;
		} else {
			head = head.next;
		}
		return head;
	}

	public static void main(String[] args) {
		RemoveNthNodeFromEndofList so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
//		so.debug4();
	}

	@Override
	public void debug(ListNode head, int n) {
		// TODO Auto-generated method stub

	}


}
